package ai.fabu.dataplatform.service.GRPCServer;

import ai.fabu.dataplatform.GetIndexRequest;
import ai.fabu.dataplatform.GetIndexResponse;
import ai.fabu.dataplatform.Index;
import ai.fabu.dataplatform.IndexDataServiceGrpc;
import ai.fabu.dataplatform.bean.util.IndexData;
import ai.fabu.dataplatform.constant.ResultCode;
import ai.fabu.dataplatform.help.Result;
import ai.fabu.dataplatform.service.ApiService;
import io.grpc.Status;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import net.devh.springboot.autoconfigure.grpc.server.GrpcService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

import static ai.fabu.dataplatform.constant.ConstantValue.BAG_TYPE;
import static ai.fabu.dataplatform.constant.ConstantValue.GROUP_TYPE;
import static ai.fabu.dataplatform.constant.ConstantValue.TOPIC_TYPE;

@Slf4j
public class DataPlatformIndexDataServer {
    @GrpcService(IndexDataServiceGrpc.class)
    private static class DataPlatformIndexDataImpl extends IndexDataServiceGrpc.IndexDataServiceImplBase{
        @Autowired
        ApiService apiService;

        private void getIndex(GetIndexRequest request, StreamObserver<GetIndexResponse> responseObserver, int type){
            Index index = request.getIndex();
            IndexData indexData = new IndexData(index.getGroupName(), index.getBagName(), index.getTopicName(), index.getFrameNo());
            Result<List<IndexData>> result;
            try {
                result = apiService.getIndex(indexData, type);
            }
            catch (Exception e){
                FunctionUtil.errorHandler(responseObserver, "getIndex", ResultCode.SYSTEM_ERROR.getMsg());
                throw e;
            }
            if(result.getCode() == ResultCode.SUCCESS.getCode()){
                List<Index> indexList = new ArrayList<>();
                List<IndexData> indexDataList = result.getData();
                indexDataList.forEach(data -> indexList.add(Index.newBuilder().setGroupName(data.getGroupName())
                        .setBagName(data.getBagName()).setTopicName(data.getTopicName()).setFrameNo(data.getFrameNo()).build()));
                GetIndexResponse response = GetIndexResponse.newBuilder().addAllIndex(indexList).build();
                responseObserver.onNext(response);
                responseObserver.onCompleted();
            }
            else
                FunctionUtil.errorHandler(result, responseObserver, "getIndex");
        }

        @Override
        public void getGroupIndex(GetIndexRequest request, StreamObserver<GetIndexResponse> responseObserver) {
            getIndex(request, responseObserver, GROUP_TYPE);
        }

        @Override
        public void getBagIndex(GetIndexRequest request, StreamObserver<GetIndexResponse> responseObserver) {
            getIndex(request, responseObserver, BAG_TYPE);
        }

        @Override
        public void getTopicIndex(GetIndexRequest request, StreamObserver<GetIndexResponse> responseObserver) {
            getIndex(request, responseObserver, TOPIC_TYPE);
        }
    }
}
